{include file='header'/}
<body class="pear-container pear-admin">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="munu-table" lay-filter="munu-table"></table>
    </div>
</div>
<script type="text/html" id="add_toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md"
            data-open="<?php echo url('auth_rule/add'); ?>"
            data-title="{:__('Add')}{:__('AuthRuleMenu')}">
        <i class="layui-icon layui-icon-add-1"></i>
        {:__('Add')}{:__('AuthRuleMenu')}
    </button>
</script>

<script type="text/html" id="iconTpl">
    <i class="layui-inline layui-icon {{d.icon}}"></i>
</script>

<script type="text/html" id="optionTpl">
    <a class="layui-btn layui-btn-xs layui-btn-normal" data-open="<?php echo url('auth_rule/add'); ?>?id={{d.id}}"
       data-title="{:__('Add')}{:__('Child')}" data-full="true" data-reload="1">{:__('Add')}{:__('Child')}</a>
    <a class="layui-btn layui-btn-xs layui-btn-success" data-open="<?php echo url('auth_rule/edit'); ?>?id={{d.id}}"
       data-title="{:__('Edit')}" data-full="true" data-reload="2">{:__('Edit')}</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" data-token="{:token()}"
       data-delete="<?php echo url('auth_rule/delete'); ?>?id={{d.id}}"
       data-title="{:__('AreYouSure %s',[__('Del')])}" data-reload="1">{:__('Del')}</a>
</script>

<script type="text/html" id="barDemo">
    <button class="pear-btn pear-btn-primary pear-btn-sm" title="{:__('Edit')}"
            data-open="<?php echo url('auth_rule/edit'); ?>?id={{d.id}}"
            data-title="{:__('Edit')}">
        <i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" title="{:__('Del')}" data-token="{:token()}"
            data-delete="<?php echo url('auth_rule/delete'); ?>?id={{d.id}}"
            data-title="{:__('AreYouSure %s',[__('Del')])}" data-reload="2">
        <i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

{include file='footer'/}

<script>
  layui.use(['table', 'treetable'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var treetable = layui.treetable;

    // 渲染表格
    layer.load(2);
    var insTb = treetable.render({
      treeColIndex: 1,
      treeSpid: 0,
      treeIdName: 'id',
      treePidName: 'pid',
      elem: '#munu-table',
      id: 'testReload',
      toolbar: '#add_toolbar',
      defaultToolbar: [{
        layEvent: 'refresh',
        icon: 'layui-icon-refresh',
        title: "{:__('Refresh')}"
      }, 'filter', 'exports'],
      method: 'get',
      url: "{:url('auth_rule/index')}",
      page: false,
      cols: [[
        {field: 'id', width: 80, title: 'ID', align: 'center'},
        {field: 'title', minWidth: 200, title: "{:__('AuthRuleTitle')}"},
        {field: 'lang_key', minWidth: 100, title: "{:__('LangKey')}"},
        {field: 'icon', width: 100, title: "{:__('AuthRuleIcon')}", templet: '#iconTpl', align: 'center'},
        {field: 'node', title: "{:__('AuthRuleNode')}"},
        {
          field: 'is_menu',
          width: 130,
          title: "{:__('IsMenu')}",
          align: 'center',
          templet: function (d) {
            if (d.is_menu === 1) {
              return '<span class="layui-badge layui-bg-red">{:__(\'Yes\')}</span>';
            } else {
              return '<span class="layui-badge-rim layui-bg-cyan">{:__(\'No\')}</span>';
            }
          }
        },
        {
          field: 'is_home',
          width: 130,
          title: "{:__('AuthRuleType')}",
          align: 'center',
          templet: function (d) {
            if (d.pid === 0) {
              return '<span class="layui-badge layui-bg-orange">{:__("Module")}</span>';
            } else {
              return '<span class="layui-badge-rim">{:__("Page")}</span>';
            }
          }
        },
        {field: 'sort', width: 80, title: "{:__('Sort')}", edit: 'text', align: 'center'},
        {
          width: 200,
          title: "{:__('Operation')}",
          templet: '#optionTpl',
          align: 'center',
        }
      ]],
      done: function () {
        layer.closeAll('loading');
      }
    });

    /* 监听单元格编辑 */
    table.on('edit(munu-table)', function (obj) {
      var value = obj.value
        , data = obj.data
        , field = obj.field;
      $.post("{:url('auth_rule/modify')}", {
        id: data.id,
        val: value,
        field: field,
        no_need__token__: 1
      }, function (res) {
        if (res.code === 200) {
          layer.msg(res.msg, {time: 1500, icon: 1}, function () {
            window.location.reload();
          });
        } else {
          layer.msg(res.msg, {time: 1500, icon: 2});
        }
      })
    });
    //监听行工具事件
    table.on('toolbar(munu-table)', function (obj) {
      if (obj.event === 'refresh') {
        window.location.reload();
      }
    });

  });
</script>
